﻿(function ($) {
    $.fn.horizontalSplitContainer = function (option) {
        option = $.extend({
            auto: true,
            items: []
            /*
            {item:$("#item1"),ratio:1}
            ratio: 1(1%) , "#1"(1px) , *(剩余比例，只能有一个)
            */
        }, option);
        return this.each(function () {
            var container = $(this);
            var settings = $.extend({}, option);
            if (settings.auto) {
                settings.items = [];
                var children = $(this).children("[ratio]");
                children.each(function () {
                    var item = { item: $(this), ratio: $(this).attr("ratio") };
                    settings.items.push(item);
                });
            }
            var helper = new $.horizontalSplitContainerHelper(container, settings.items);
            helper.valid();
            container.layout(function () { helper.layout() });
            container.layout();
            container.helper("horizontalSplitContainer", helper);
            //            $(window).resize(function () {
            //                helper.layout();
            //            });
            //            container.bind("layout", function () {
            //                helper.layout();
            //            });
        });
    }

    $.horizontalSplitContainerHelper = function (_container, _items) {
        this.container = _container;
        this.items = _items;
        this.valid = function () {
            var autoratioitem = 0;
            for (var i = 0; i < this.items.length; i++) {
                var ratio = this.items[i].ratio;
                if (ratio == "*") {
                    autoratioitem++;
                }
            }
            if (autoratioitem > 1) {
                throw new Error("自动比例的item只能有1个");
            }
        },
        this.layout = function () {
            var width = this.container.width();
            var realwidth = width;
            for (var i = 0; i < this.items.length; i++) {
                realwidth -= this.items[i].item.outerWidth() - this.items[i].item.width()
            }
            var usedwidth = 0;
            for (var i = 0; i < this.items.length; i++) {
                var ratio = this.items[i].item.attr("ratio");
                if (ratio.substr(0, 1) == "#") {
                    var width = Math.round(ratio.substring(1) * 1);
                    this.items[i].item.width(width);
                    usedwidth += width;
                } else if (!isNaN(ratio)) {
                    var width = Math.round(realwidth * ratio / 100);
                    this.items[i].item.width(width);
                    usedwidth += width;
                }
            }
            for (var i = 0; i < this.items.length; i++) {
                var ratio = this.items[i].item.attr("ratio");
                if (ratio == "*") {
                    var width = realwidth - usedwidth;
                    this.items[i].item.width(width);
                    usedwidth += width;
                }
            }
            if (realwidth - usedwidth > 0 && this.items.length > 0) {
                var lastitem = this.items[this.items.length - 1];
                lastitem.item.width(lastitem.item.width() + (realwidth - usedwidth));
            }
        }
    }
})(jQuery);